#ifndef A2ACC_H
#define A2ACC_H

#ifndef H_A2ACC
#define H_A2ACC
/* COMMON STRUCTURES AND CONSTANTS DESCRIPTION */

#ifndef A2DDT_H
#include "a2ddt.h"
#endif

/*               BASE  ACCESS  SYSTEM  ERROR  CODES                 */
/*======================== serious errors ==========================*/
#define EA_OM  (-81)                           /* not enough memory */
#define EA_UV  (-82)               /* updating of structural vertex */
#define EA_MK  (-84)                            /* invalid key type */
#define EA_RV  (-85)          /* reading value of structural vertex */
#define EA_AC  (-86)                      /* invalid operation code */
#define EA_MS  (-87)                       /* invalid movement code */
#define EA_KL  (-88)           /* there are only keys on this level */
#define EA_UK  (-89)             /* improper action with key vertex */
#define EA_KRD (-90)             /* operation may violate key order */
#define EA_PC  (-91)                   /* erroneous pointer to copy */
#define EA_RT  (-92)                /* error in the path of pointer */
#define EA_PF  (-93)                    /* error in data conversion */
#define EA_CC  (-94)        /* copying doesn't correspond to scheme */
#define EA_CT  (-95)                  /* incorrect pointer or table */
#define EA_DK  (-96)                    /* improper deleting of key */


/* ACCESS TO THE CONTROL BLOCK DATA AND ENVIRONMENT PARAMETERS */

/*---- pointer to control block of process in data tree -----*/
#define ACSPTREE AcsPTREE
#define AcsPTREE(pacc) pacc->pt
/* pointer to cnt. block of process in data description tree */
#define ACSPDDT AcsPDDT
#define AcsPDDT(pacc) pacc->pd
/*------------- level number from base root -----------------*/
#define ACSLVL AcsLVL
#define AcsLVL(pacc) pacc->lvl
/*----------- level number from stack header ----------------*/
#define ACSGLVL AcsGLVL
#define AcsGLVL(pacc) pacc->gllvl
/*------------------ not 0 if key level ---------------------*/
#define ACSKLVL AcsKLVL
#define AcsKLVL(pacc) pacc->keylvl
/*---------------- not 0 if vertex is key -------------------*/
#define ACSKEY AcsKEY
#define AcsKEY(pacc) pacc->keyno
/*---------------------- value type -------------------------*/
#define ACSVTYPE AcsVTYPE
#define AcsVTYPE(pacc) pacc->type
/*--------------- key type (for key level) ------------------*/
#define ACSKTYPE AcsKTYPE
#define AcsKTYPE(pacc) pacc->ktype
/*----------------- output format adress --------------------*/
#define ACSPF AcsPF
#define AcsPF(pacc) (pacc->pd->vx[pacc->pd->icl].pf)
/*--- ddt-key (internal system identifier) of the vertex ----*/
#define ACSDKEY AcsDKEY
#define AcsDKEY(pacc) pacc->dk
/*------------------- set message level ---------------------*/
#define ACSERMSGP AcsERMSGP
#define AcsERMSGP(pacc,t) pacc->emsg=t
/*------------------ read message level ---------------------*/
#define ACSERMSG AcsERMSG
#define AcsERMSG(pacc) pacc->emsg
/*-------------------- set trace level ----------------------*/
#define ACSTRACEP AcsTRACEP
#if !defined(__FOR_WINDOWS__) && !defined(__FOR_WIN32__)
# define AcsTRACEP(pacc,t,s)                       \
     a2acctrp = a2acctr,                           \
     strncpy(pacc->trid, s, sizeof(pacc->trid)-1), \
     pacc->trc = t;
#else
# define AcsTRACEP(pacc,t,s)                       \
     strncpy(pacc->trid, s, sizeof(pacc->trid)-1), \
     pacc->trc = t;
#endif
/*------------------- read trace level ----------------------*/
#define ACSTRACE AcsTRACE
#define AcsTRACE(pacc) pacc->trc
/*-- set regime of writing while passing through references -*/
#define ACSKRFTRP AcsKRFTRP
#define AcsKRFTRP(pacc,t) pacc->rftr=t
/*- read regime of writing while passing through references -*/
#define ACSKRFTR AcsKRFTR
#define AcsKRFTR(pacc) (pacc->rftr)
/*------- set regime of passing of reference vertex ---------*/
#define ACSRSTOPP AcsRSTOPP
#define AcsRSTOPP(pacc,t) pacc->rstop=t
/*------ read regime of passing of reference vertex ---------*/
#define ACSRSTOP AcsRSTOP
#define AcsRSTOP(pacc) (pacc->rstop)
/*------------- set regime of index maintenance -------------*/
#define ACSATRP AcsATRP
#define AcsATRP(pacc,t) pacc->atr=t
/*------------ read regime of index maintenance -------------*/
#define ACSATR AcsATR
#define AcsATR(pacc) (pacc->atr)
/*--------- size of buffer for translated references --------*/
#define ACSBSIZE AcsBSIZE
#define AcsBSIZE(i) {}
/*------------- current transaction type --------------------*/
#define ACSTRANS AcsTRANS
#define AcsTRANS(pacc) (pacc->pt->pdf->trType1)


/*   MACROOPERATIONS  FOR  DATA  MANIPULATION  */

#if (defined(__FOR_WINDOWS__) || defined(__FOR_WIN32__)) && defined(H_A2NIKA)
# define ACSSKD(e,p,mv,ac,kt,k,dt,d) {*e=AcsSKD(p,mv,ac, kt,k, dt,d);}
# define ACSSK(e,p,mv,ac,kt,k)       {*e=AcsSK(p, mv,ac, kt,k);      }
# define ACSSD(e,p,mv,ac,dt,d)       {*e=AcsSD(p, mv,ac, dt,d);      }
# define ACSS(e,p,mv,ac)             {*e=AcsS(p, mv, ac);            }
# define ACSCPY(e,mv,p1,p2)          {*e=AcsCPY(mv, p1, &p2);        }
# define ACSDUP(e,ptr1,ptr2)         {*e=AcsDUP(ptr1, &ptr2);        }
# define ACSDLT(e,ptr1,ptr2)         {*e=AcsDLT(ptr1, &ptr2);        }
# define ACSSAV(err,pt)              {*err=AcsSAV(pt);               }
# define ACSRST(err,pt)              {*err=AcsRST(pt);               }
# define ACSCP(err,pac,fc)            AcsCP(pac)
#else
# define ACSSKD(e,p,mv,ac,kt,k,dt,d) {*e=a2accc(p,mv,ac, kt,k, dt,d,3);}
# define ACSSK(e,p,mv,ac,kt,k)       {*e=a2accc(p,mv,ac,kt,k,0,NULL,1);}
# define ACSSD(e,p,mv,ac,dt,d)       {*e=a2accc(p,mv,ac,0,NULL,dt,d,2);}
# define ACSS(e,p,mv,ac)           {*e=a2accc(p,mv,ac,0,NULL,0,NULL,0);}
# define ACSCPY(e,mv,p1,p2)       {*e=a2accc(p1,S_O,mv,0,NULL,0,&p2,2);}
# define ACSDUP(e,ptr1,ptr2) {*e=a2accc(ptr1,S_O,D_P,0,NULL,0,&ptr2,2);}
# define ACSDLT(e,ptr1,ptr2) {*e=a2accc(ptr1,S_O,R_C,0,NULL,0,&ptr2,2);}
# define ACSSAV(err,pt) {*err=a2accc(pt, S_O, S_P, 0, NULL, 0,NULL, 0);}
# define ACSRST(err,pt) {*err=a2accc(pt, S_O, R_P, 0, NULL, 0,NULL, 0);}
# define ACSCP(err,pac,fc)                a2dacp(err, pac->pt->pdf, fc)
#endif

/*------------------------ FLAGS FOR A2ACCBT -----------------------*/
#define ACS_TERM     1
#define ACS_NTERM    2
#define ACS_EMP      4
#define ACS_NEMP     8
#define ACS_NTERMF  16
#define ACS_NTERMB  32

#define ACS_UP 2
#define ACS_RY 1
#define ACS_DO 3
/*------------------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(__FOR_WINDOWS__) && !defined(__FOR_WIN32__)
/*----------------- internal trace utilities -----------------------*/
extern int a2acctr( int err, ACCESS *pa, TCOMMAND *pus ) ;
extern int (*a2acctrp)( int err, ACCESS *pa, TCOMMAND *pus );
#endif

/*  USER  ENTREES  DEFINITION  */

#if (defined(__FOR_WINDOWS__) || defined(__FOR_WIN32__)) && defined(H_A2NIKA) && !defined(SOURCE_A2ACCESS)
# define a2accop(e,dfile,tfile,disp,blks,psw,user) AcsOpen(e,dfile,tfile,disp)
# define a2acccl AcsClose
#else
 ACCESS *a2accop (
    int *pe, char *dod,char *tre, int disp, int *pblks, void *psw,void *user
 );
 int a2acccl ( ACCESS *ptacc ) ;
#endif

#ifndef H_A2NIKA

/*------------------ access to the r/w operations ------------------*/
int a2acc( ACCESS *puacc, TCOMMAND *pucom ) ;
int a2accc( ACCESS *ptr, char m1, char a,
	    char kt1, void *k1, char vt1, void *v1, char arg ) ;
void a2accbt(int *rc, ACCESS *pb, char kind,
	      unsigned top, unsigned bottom, unsigned *pvar);

int a2accUpdAtr(ACCESS *pacc);
int a2accAtrFill (ACCESS *pacc, int ClearINDEX);

int a2accLeftTracking (
   ACCESS *pacc, int flags, char *workbuf256,
   int (*callBack)(ACCESS *pacc, int mov, char *workbuf256)
);

enum a2accLeftTracking_flags {
   ACS_SkipLVL0  = 1, /* doesn't execute <callBack> for LEVEL 0 */
   ACS_SkipINDEX = 2, /* .................... for INDEX subtree */
   ACS_SkipEMPTY = 4, /* ................... for empty vertices */
   ACS_SkipREF   = 8, /* ..................... for REF vertices */
   ACS_SkipKEY   = 16,/* ............................. for KEYS */
   ACS_SkipUPSTK = 32,/* ......................... for UP STACK */
   ACS_IgnoreERR = 64 /* Ignore serious errors in source tree   */
};

/*-- <mov> in <callBack> may be one of follows: D_K, L_K, U_S --*/

int a2accCopy(ACCESS *paccSrc,ACCESS *paccDst, int IgnErr);

#endif

#ifdef __cplusplus
}
#endif

#endif /* H_A2ACC  */
#endif /* A2ACC_H  */
